package me.thinkjet.controller.sso;

import me.thinkjet.sso.server.authentication.Authentication;
import me.thinkjet.sso.server.service.CentralAuthenticationService;
import me.thinkjet.sso.server.ticket.ServiceTicket;
import me.thinkjet.sso.server.ticket.exception.TicketException;

import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;

@ControllerBind(controllerKey = "/auth", viewPath = "auth")
public class AuthController extends Controller {
	CentralAuthenticationService centralAuthenticationService = new CentralAuthenticationService();
	private final static String CLIENT_ID = "client";
	private final static String KEY_NAME = "key";

	public void index() {
		final String service = this.getPara(CLIENT_ID);
		final String key = this.getPara(KEY_NAME);
		String serviceTicketId = this.getPara(ServiceTicket.TICKET_NAME);
		try {
			Authentication authentication = centralAuthenticationService
					.validateServiceTicket(serviceTicketId, service, key);
			this.renderJson(authentication);
		} catch (TicketException e) {
			e.printStackTrace();
		}

	}
}
